package openase.audiosearch.client.search.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;

import openase.audiosearch.client.resources.Resources;
import openase.audiosearch.client.search.controller.AudioSearchController;


public class AudioSearchFrame extends JFrame {

	private static final long serialVersionUID = -3678964999128615909L;

	private AudioSearchController controller;

	private JButton btnRecord = new JButton("| Record", Resources.getIcon(Resources.MICROPHONE_URL));

	private JTextField txtDuration = new JTextField("30", 3);
	private JTextArea txtResults = new JTextArea();

	public AudioSearchFrame() {
		super("Open-ASE | Search client");
		setContentPane(new JPanel(new BorderLayout(5, 5)));
		controller = new AudioSearchController();

		buildComponents();

		pack();
		setResizable(false);
	}

	private void buildComponents() {
		txtResults.setColumns(50);
		txtResults.setRows(20);
		txtResults.setEditable(false);

		JPanel panelDuration = new JPanel();
		JLabel lblDuration = new JLabel("Record duration (in seconds)");
		panelDuration.add(lblDuration, BorderLayout.WEST);
		panelDuration.add(txtDuration, BorderLayout.EAST);

		JPanel panelInput = new JPanel(new BorderLayout());
		panelInput.add(panelDuration, BorderLayout.NORTH);

		JPanel panelButton = new JPanel();
		panelButton.add(btnRecord);

		panelInput.add(panelButton, BorderLayout.CENTER);
		panelInput.setBorder(new TitledBorder("Record"));

		JScrollPane panelResults = new JScrollPane(txtResults);
		panelResults.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		panelResults.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panelResults.setBorder(new TitledBorder("Search results"));

		this.add(panelInput, BorderLayout.NORTH);
		this.add(panelResults, BorderLayout.SOUTH);

		btnRecord.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				btnRecord.setEnabled(false);

				final Runnable process = new Runnable() {

					public void run() {
						try {
							String strDuration = txtDuration.getText();
							int durationInSecs = Integer.parseInt(strDuration);
							if (durationInSecs < 1) {
								throw new IllegalArgumentException("Duration must be positive: " + durationInSecs);
							}

							txtResults.setText("Recording " + durationInSecs + " seconds of audio...");

							byte[] recordedAudio = controller.record(durationInSecs);

							txtResults.setText("Searching...");
							String results = controller.search(recordedAudio);

							txtResults.setText(results);

							JOptionPane.showMessageDialog(AudioSearchFrame.this, "Search complete!");
						} catch (Throwable t) {
							txtResults.setText("Error: " + t.getMessage());
							JOptionPane.showMessageDialog(AudioSearchFrame.this, t.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						} finally {
							btnRecord.setEnabled(true);
						}
					}
				};

				Executors.defaultThreadFactory().newThread(process).start();

			}
		});

	}
}
